#include "unit.h"
#include "game.h"
#include "server.h"

#include <QDebug>

Unit::Unit(const qint32 ID)
   : Entity(ID)
{
   mBaseStats.type = ENTITY_UNIT;
}

void Unit::setKind(const qint32 kind)
{
   mBaseStats.kind = kind;
   mSpeed = (MAX_UNIT_SPEED-mBaseStats.kind)*10;
}

void Unit::setSize(const qint32 size)
{
   mBaseStats.size = size;
   mHealth = mBaseStats.size*10;
}


void Unit::setBaseStats(const EntityBaseStats &stats)
{
   Entity::setBaseStats(stats);
   mHealth = mBaseStats.size*10;
   mSpeed = (MAX_UNIT_SPEED-mBaseStats.kind)*10;
}

void Unit::onAddToGame()
{
   //path = mGame->path();
}

void Unit::tick(const qreal timeDelta)
{
   mVelocity.normalize();
   mVelocity *= (mSpeed * timeDelta);

   QVector2D lastPosition = mPosition;
   mPosition += mVelocity;

   mUpdated = (mPosition != lastPosition);
   //qDebug() << "Unit::tick: id=" << mID << "pos=" << mPosition << "updated=" << mUpdated << "timedelta=" << timeDelta;


   bool validGoal = false;
   while (!validGoal)
   {
      if (path.isEmpty())
      {
         setActive(false);
         return;
      }
      QVector2D pathLoc(path.front());
      mVelocity = pathLoc - mPosition;

      if (mVelocity.length() <= 1)
         path.pop_front();
      else
         validGoal = true;
   }

}

void Unit::think()
{
   setNextThinkTime(mGame->currentTime().addMSecs(500));
}
